home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2.0 - Programmer's Utilities Power Pack / Delphi 2.0 Programmer's Utilities Power Pack.iso / a_to_d / delftips / ti2849.asc < prev    next >
Text File  |  1996-09-15  |  6KB  |  183 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.   PRODUCT  :  Delphi                                 NUMBER  :  2849
  8.   VERSION  :  All
  9.        OS  :  Windows
  10.      DATE  :  August 17, 1995                          PAGE  :  1/3
  11.  
  12.     TITLE  :  Determining Record Number In A dBASE Table
  13.  
  14.  
  15.  
  16.  
  17. dBASE tables employ a fairly static record numbering system. The record
  18. number for a given record reflects its physical position in the table
  19. file. These record number are not subject to change dependent on the
  20. filtering of data or index ordering. For instance, a record that is the
  21. first record stored in the .DBF file would be record number 1. It is
  22. possible that, through the ordering of an index, this record may be
  23. displayed as the last of 100 records. In such a case, its record would
  24. remain the same -- one -- and would not be changed to 100 to reflect its
  25. position in the index ordered data set. This is in contrast with Paradox
  26. tables, where there is a sequence number. The Paradox sequence number is
  27. like the dBASE record number except that it is much more fluid and the
  28. number for a given record will reflect its position relative to the data
  29. set. That is, a record may not always have the same sequence number given
  30. filtering of the data set to reduce the number of records or when an index
  31. is active that may change the displayed order of the record.
  32.  
  33. In database applications created with Delphi and the Borland Database
  34. Engine (BDE), there is no provision built into the stock data components
  35. for extracting or determining the record for dBASE tables. Such an opera-
  36. tion is, however, possible by making a call from the application a BDE
  37. function.
  38.  
  39. There are a number of BDE functions that will return information about the
  40. current dBASE record, such as the record number. Basically, any function
  41. that fills a BDE pRECProps structure would suffice. Such BDE functions
  42. include DbiGetRecord, DbiGetNextRecord, and DbiGetPriorRecord. Of course,
  43. only the first of these functions really applies to retrieving information
  44. about the current record. The other two move the record pointer when in-
  45. voked, similar in effect to the Next and Prior methods of the TTable or
  46. TQuery components.
  47.  
  48. The pRECProps structure consists of the fields:
  49.  
  50.   iSeqNum: type LongInt; specifies the sequence number of the record
  51.     (relative to the data set, including filtering and index ordering);
  52.     applicable if the table type supports sequence numbers (Paradox only).
  53.  
  54.   iPhyRecNum: type LongInt; specifies the record number for the record;
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.   PRODUCT  :  Delphi                                 NUMBER  :  2849
  69.   VERSION  :  All
  70.        OS  :  Windows
  71.      DATE  :  August 17, 1995                          PAGE  :  2/3
  72.  
  73.     TITLE  :  Determining Record Number In A dBASE Table
  74.  
  75.  
  76.  
  77.  
  78.     applicable only when the table type supports physical record numbers
  79.     (dBASE only).
  80.  
  81.   bRecChanged: type Boolean; not currently used.
  82.  
  83.   bSeqNumChanged: type Boolean; not currently used.
  84.  
  85.   bDeleteFlag: type Boolean; indicates whether the record is deleted;
  86.     applicable only for table types that support soft-deletes (dBASE
  87.     only).
  88.  
  89. One of these BDE functions may be invoked in a Delphi application to fill
  90. this structure, from which the physical record number may be retrieved.
  91. Below is an example of the DbiGetRecord function used for this purpose.
  92.  
  93.   function RecNo(ATable: TTable): LongInt;
  94.   var
  95.     R: RECProps;
  96.     rslt: DbiResult;
  97.     Error: array [0..255] of Char;
  98.   begin
  99.     ATable.UpdateCursorPos;
  100.     rslt := DbiGetRecord(ATable.Handle, dbiNoLock, nil, @R);
  101.     if rslt = DBIERR_NONE then
  102.       Result := R.iPhyRecNum
  103.     else begin
  104.       DbiGetErrorString(rslt, Error);
  105.       ShowMessage(StrPas(Error));
  106.       Result := -1;
  107.     end;
  108.   end;
  109.  
  110. As with invoking any BDE function in a Delphi application, the BDE wrapper
  111. units DbiTypes, and DbiErrs, DbiProcs must be included in the Uses section
  112. of the unit in which the BDE function will be invoked (the Uses section
  113. not shown here). To make this function more transportable, it does not
  114. reference the subject TTable component directly, but a reference to the
  115. TTable is passed as a parameter. If this function is used in a unit that
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.   PRODUCT  :  Delphi                                 NUMBER  :  2849
  130.   VERSION  :  All
  131.        OS  :  Windows
  132.      DATE  :  August 17, 1995                          PAGE  :  3/3
  133.  
  134.     TITLE  :  Determining Record Number In A dBASE Table
  135.  
  136.  
  137.  
  138.  
  139. does not reference the Delphi units DB and DBTables, they must be added so
  140. that references to the TTable component will be valid.
  141.  
  142. The UpdateCursorPos method of the TTable is called to ensure that the
  143. record number current in the TTable component is synchronized with that
  144. of the underlying table.
  145.  
  146. BDE functions do not in themselves cause an exception if they fail.
  147. Rather, they return a value of BDE type DbiResult that indicates the succ-
  148. ess or failure of the intended operation. This return value must then be
  149. retrieved and evaluated by the front-end application, and the appropriate
  150. action taken. A result other than DBIERR_NONE indicates an unsuccessful
  151. execution of the function. An extra step may be taken (as in the example
  152. above) to query the BDE to translate an error code into a readable mess-
  153. age with the BDE function DbiGetErrorString. In this example, the return
  154. value from the invoking of DbiGetRecord is stored in the variable rslt,
  155. and then compared against DBIERR_NONE to determine the success of the
  156. function call.
  157.  
  158. If the call to DbiGetRecord succeeds, the physical record number from the
  159. iPhyRecNum field of the pRECProps structure is stored to the variable
  160. Result, which is the function's return value. To indicate when the 
  161. function has failed (i.e., the involing of the DbiGetRecord function
  162. failed), a value of negative one is returned instead of the record
  163. number. This value is purely arbitrary, and any value of a compatible
  164. type may be used at the discretion of the programmer.
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179. DISCLAIMER: You have the right to use this technical information
  180. subject to the terms of the No-Nonsense License Statement that
  181. you received with the Borland product to which this information
  182. pertains.
  183.